package leetcode.weekly.week295;

import java.util.Stack;

//Solution3Test
public class Solution3 {

	// Solution3Test
	public int totalSteps(int[] nums) {
		Stack<int[]> stack = new Stack<>();
		int ans = 0;
		for (int i = 0; i < nums.length; i++) {
			int num = nums[i];
			int max = 0;
			// [7,14,4,14,13,2,6,13]
			while (!stack.isEmpty() && nums[stack.peek()[0]] <= num) {
				int[] cur = stack.pop();
				max = Math.max(max, cur[1]);
			}
			ans = Math.max(ans, max);
			max += !stack.isEmpty() ? 1 : 0;
			stack.add(new int[] { i, max });
		}
		int max = 0;
		while (!stack.isEmpty()) {
			int[] cur = stack.pop();
			max = Math.max(max, cur[1]);
		}
		ans = Math.max(ans, max);
		return ans;
	}

}
